package com.tuanzi.loan.business.repository.xhh.settlementsystem.impl;

import com.tuanzi.loan.business.repository.xhh.settlementsystem.custom.XhhSettlementBillRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementBillRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementBillResponse;
import org.apache.commons.lang3.time.DateUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.XhhLoanSettlementBill.XHH_LOAN_SETTLEMENT_BILL;

/**
 * @author caiwanlin
 * @create 2018-08-24 16:08
 */
public class XhhSettlementBillRepositoryImpl extends JOOQBaseRepositoryPlus implements XhhSettlementBillRepositoryCustom {

    @Override
    public Page<XhhSettlementBillResponse> list(XhhSettlementBillRequest searchRequest, Pageable pageable) {
        Long companyId = searchRequest.getCompanyId();
        Date startTime = searchRequest.getStartTime();
        String creator = searchRequest.getCreator();
        Integer billStatus = searchRequest.getBillStatus();
        Date endTime = searchRequest.getEndTime();
        if (endTime != null){
            endTime = DateUtils.addHours(endTime,23);
            endTime = DateUtils.addMinutes(endTime,59);
            endTime = DateUtils.addSeconds(endTime,59);
        }
        fields(XHH_LOAN_SETTLEMENT_BILL.fields());
        eq(XHH_LOAN_SETTLEMENT_BILL.COMPANY_ID,companyId);
        eq(XHH_LOAN_SETTLEMENT_BILL.BILL_STATUS,billStatus);
        eq(XHH_LOAN_SETTLEMENT_BILL.CREATOR,creator);
        between(XHH_LOAN_SETTLEMENT_BILL.CREATE_TIME,startTime,endTime);
        orderBy(XHH_LOAN_SETTLEMENT_BILL.CREATE_TIME, Sort.Direction.DESC);

        return search(pageable, record -> {
            XhhSettlementBillResponse response = new XhhSettlementBillResponse();
            response.setId(record.get(XHH_LOAN_SETTLEMENT_BILL.ID));
            response.setCompanyId(record.get(XHH_LOAN_SETTLEMENT_BILL.COMPANY_ID));
            response.setCompanyName(record.get(XHH_LOAN_SETTLEMENT_BILL.COMPANY_NAME));
            response.setRecordType(record.get(XHH_LOAN_SETTLEMENT_BILL.RECORD_TYPE));
            response.setBillContent(record.get(XHH_LOAN_SETTLEMENT_BILL.BILL_CONTENT));
            response.setTaxNumber(record.get(XHH_LOAN_SETTLEMENT_BILL.TAX_NUMBER));
            response.setBillType(record.get(XHH_LOAN_SETTLEMENT_BILL.BILL_TYPE));
            response.setBankAccount(record.get(XHH_LOAN_SETTLEMENT_BILL.BANK_ACCOUNT));
            response.setBankAddress(record.get(XHH_LOAN_SETTLEMENT_BILL.BANK_ADDRESS));
            response.setBankNumber(record.get(XHH_LOAN_SETTLEMENT_BILL.BANK_NUMBER));
            response.setBillAddress(record.get(XHH_LOAN_SETTLEMENT_BILL.BILL_ADDRESS));
            response.setReceiveName(record.get(XHH_LOAN_SETTLEMENT_BILL.RECEIVE_NAME));
            response.setReceiveAddress(record.get(XHH_LOAN_SETTLEMENT_BILL.RECEIVE_ADDRESS));
            response.setReceivePhone(record.get(XHH_LOAN_SETTLEMENT_BILL.RECEIVE_PHONE));
            response.setBillStatus(record.get(XHH_LOAN_SETTLEMENT_BILL.BILL_STATUS));
            response.setBillAmount(record.get(XHH_LOAN_SETTLEMENT_BILL.BILL_AMOUNT));
            response.setCheckAccount(record.get(XHH_LOAN_SETTLEMENT_BILL.CHECK_ACCOUNT));
            response.setCheckName(record.get(XHH_LOAN_SETTLEMENT_BILL.CHECK_NAME));
            response.setBillImageUrl(record.get(XHH_LOAN_SETTLEMENT_BILL.BILL_IMAGE_URL));
            response.setRemark(record.get(XHH_LOAN_SETTLEMENT_BILL.REMARK));
            response.setCreator(record.get(XHH_LOAN_SETTLEMENT_BILL.CREATOR));
            response.setCreateTime(record.get(XHH_LOAN_SETTLEMENT_BILL.CREATE_TIME));
            response.setUpdator(record.get(XHH_LOAN_SETTLEMENT_BILL.UPDATOR));
            response.setUpdateTime(record.get(XHH_LOAN_SETTLEMENT_BILL.UPDATE_TIME));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return XHH_LOAN_SETTLEMENT_BILL;
    }
}
